package com.example.demo_mysql_mybatisplus.util.easyexcel.converters;


import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.example.demo_mysql_mybatisplus.util.easyexcel.enums.BoolEnum;

/**
 * BoolEnum的转换器
 * @author Administrator
 */
public class BoolEnumConverter implements Converter<BoolEnum> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return BoolEnum.class;//对象属性类型
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;//CellData属性类型
    }

    @Override
    public BoolEnum convertToJavaData(ReadConverterContext<?> context) {
        String cellStr = context.getReadCellData().getStringValue();
        if (StrUtil.isNotEmpty(cellStr) ) {//CellData转对象属性
            return BoolEnum.DESC_MAP.get(cellStr) ;
        } else {
            return null;
        }
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<BoolEnum> context) {
        BoolEnum cellValue = context.getValue();
        if (cellValue != null) {//对象属性转CellData
            return new WriteCellData<>(cellValue.getDesc());
        }else{
            return new WriteCellData<>("");
        }
    }
}